package com.yswl.pri.home.vedio;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import com.tencent.smtt.export.external.interfaces.JsResult;
import com.tencent.smtt.export.external.interfaces.WebResourceRequest;
import com.tencent.smtt.export.external.interfaces.WebResourceResponse;
import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;
import com.tencent.smtt.utils.TbsLog;
import com.yswl.pri.R;
import com.yswl.pri.common.BaseBrowserActivity;
import com.yswl.pri.home.vedio.utils.AnalysUtil;

import yswl.com.klibrary.util.L;

public class MeijuBrowserActivity extends BaseBrowserActivity {

    private static final String TAG = MeijuBrowserActivity.class.getSimpleName();

    public static void JumpAct(Context context, String title, String url) {
        Intent intent = new Intent(context, MeijuBrowserActivity.class);
        intent.putExtra("video_url", url);
        intent.putExtra("video_name", title);
        context.startActivity(intent);
    }

    /**
     * 作为一个浏览器的示例展示出来，采用android+web的模式
     */
    private String mSiteUrl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mSiteUrl = getIntent().getStringExtra("video_url");
        String name = getIntent().getStringExtra("video_name");
        L.e(TAG, " 网页视频地址 :" + mSiteUrl);
        setToolbarTitle(name);
        long time = System.currentTimeMillis();
        mWebView.loadUrl(mSiteUrl);
        mWebView.requestFocus();
        TbsLog.d("time-cost", "cost time: " + (System.currentTimeMillis() - time));
        mWebView.setWebViewClient(getCustomWebViewClient());
        mWebView.setWebChromeClient(getCustomWebChromeClient());
    }

    WebViewClient getCustomWebViewClient() {
        return new MyWebClient();
    }

    WebChromeClient getCustomWebChromeClient() {
        return new MyWebChromeClient();
    }


    class MyWebClient extends WebViewClient {
        @Override
        public WebResourceResponse shouldInterceptRequest(WebView webView, WebResourceRequest webResourceRequest) {
            String url = webResourceRequest.getUrl().toString();
            if (url.startsWith("http://m.592meiju.com") && url.endsWith(".html")) {//选集 刷新无效的判断
                mSiteUrl = url;
                return super.shouldInterceptRequest(webView, webResourceRequest);
            }
            if (AnalysUtil.GG(url, 2)) return new WebResourceResponse();
            L.e(TAG, "shouldInterceptRequest new : " + url);
            return super.shouldInterceptRequest(webView, webResourceRequest);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView webView, String url) {
            if (AnalysUtil.GG(url, 2)) return true;
            L.e(TAG, "shouldOverrideUrlLoading : new" + url);
            return super.shouldOverrideUrlLoading(webView, url);
        }

        @Override
        public void onLoadResource(WebView webView, String url) {
            L.e(TAG, "onLoadResource :" + url);
            super.onLoadResource(webView, url);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            L.e(TAG, "onPageFinished :" + url);
//            if (url.startsWith("http://m.592meiju.com") && url.endsWith(".html"))
//                mSiteUrl = url;
            super.onPageFinished(view, url);
        }
    }

    class MyWebChromeClient extends WebChromeClient {
        @Override
        public void onReceivedTitle(WebView webView, String s) {
            super.onReceivedTitle(webView, s);
            setToolbarTitle(s);
        }

        @Override
        public boolean onJsAlert(WebView webView, String s, String s1, JsResult jsResult) {
            return true;
        }

        @Override
        public void onProgressChanged(WebView webView, int newProgress) {
            super.onProgressChanged(webView, newProgress);
            if (null != mPageLoadingProgressBar)
                mPageLoadingProgressBar.setProgress(newProgress);
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.clear();
        menu.add(1, 10, 0, "刷新").setIcon(R.mipmap.icon_change).
                setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);
        int id = item.getItemId();
        if (id == 10) {
            mWebView.clearHistory();
            mWebView.loadUrl(mSiteUrl);
        }
        return true;
    }


    @Override
    protected void onNewIntent(Intent intent) {
        if (intent == null || mWebView == null || intent.getData() == null)
            return;
        mWebView.loadUrl(intent.getData().toString());
    }

}
